<?php
namespace app\feedback\controller;
use \app\check\controller\Check;
use \think\Db;
// 组合中点盘
class Feedback extends Check
{
//    public function _initialize()
//    {
//        parent::filter_xss();
//        if (!defined('IS_ACCESS_TOKEN_CHECKED')) {
//            parent::_initialize();// 接口调用验证
//        }
//    }

    /**
     * 用户反馈提交
     * @return json
     */
    public function push()
    {
        $data['content'] = htmlspecialchars(input('content'));
        $data['uc_id'] = htmlspecialchars(input('ucid'));
        $data['type'] = htmlspecialchars(input('type'));
        $data['job'] = htmlspecialchars(input('job'));
        $data['client'] = htmlspecialchars(input('client'));
        $data['model'] = htmlspecialchars(input('model'));
        $data['phone_version'] = htmlspecialchars(input('phone_version'));
        $data['app_version'] = htmlspecialchars(input('app_version'));
        $data['email'] = htmlspecialchars(input('email'));
        // 邮箱验证
        if ($data['email'] && !filter_var($data['email'], FILTER_VALIDATE_EMAIL))
            return json(['code'=>11101,'msg'=>'Invalid parameter.']);

        $imgs = $this->avatar();
        if(!empty($imgs['error'])) return json(['code'=>11103,'msg'=>'Image Upload Failed.']);

        $data['pics'] = json_encode($imgs['imgs']);

        if(!$data['type'] || !$data['job'] || ($data['content'] == '' && $data['pics'] == ''))
        {
            return json(['code'=>11101,'msg'=>'Invalid parameter.']);
        }
        $time = time();
        $data['create_time'] = $time;
        $data['last_time'] = $time;
        if($fid = Db::table('app_feedback')->insertGetId($data))
        {
            return json(['code'=>200,'msg'=>'Success.']);
        }

        return json(['code'=>11102,'msg'=>'Error.']);
    }

    /**
     * 获取用户反馈接口显示内容
     * @return mixed
     */
    public function get()
    {
        // 建议类型
        $type = [
            ['id'=>'1','name'=>'内容建议'],
            ['id'=>'2','name'=>'功能建议'],
            ['id'=>'3','name'=>'程序BUG'],
            ['id'=>'4','name'=>'其他']
        ];

        $job = [
            '产品使用者',
            '兴趣爱好',
            '占星师',
            '技术人员',
            '决策者',
            '管理员',
        ];

        return json(['code'=>200,'data'=>['type'=>$type,'job'=>$job],'msg'=>'ok']);
    }

    /**
     * 批量上传意见反馈图
     * @return [type] [description]
     */
    private function avatar($dir = 'uploads/feedback')
    {
        $Avatar = new \app\common\controller\Avatar();

        return $Avatar->uploads_array($dir);
    }
}